import os
import sys
from flask import Blueprint, request, render_template, jsonify
import hand_feature
import os
import logging
import subprocess

log = logging.getLogger("Core.Analysis.Processing")
INTERPRETER = "/usr/local/anaconda3/envs/cv_learn/bin/python3.9"

basedir = os.path.abspath(os.path.dirname(__file__))
UPLOADED_PATH = os.path.join(basedir, 'view/static/image')

# 创建蓝图
func = Blueprint('flask_func', __name__)

@func.route('/image_upload', methods=['POST', 'GET'])
def single_image_detect():
    if request.method == 'POST':
        f = request.files.get('file')
        file_path = os.path.join(UPLOADED_PATH, f.filename)
        f.save(file_path)

    #todo list#
    #Add YOLO process#

    obj = {
        "code": 200,
    }

    return jsonify(obj)

# 实际访问url为ip:port/v0001/hello,只接受GET方法
@func.route('/', methods=['GET'])
def func_index():
    return render_template('/index.html')

@func.route('/index.html', methods=['GET'])
def func_hello():
    return render_template('/index.html')

@func.route('/upload', methods=['GET'])
def func_upload():
    return render_template('/upload.html')

# 实际访问url为ip:port/v0001/hello,只接受GET方法
@func.route('/hand_detect/start', methods=['GET'])
def hand_detect():
    if not os.path.exists(INTERPRETER):
        log.error("Cannot find INTERPRETER at path \"%s\"." % INTERPRETER)

    processor = "hand_feature.py"
    pargs = [INTERPRETER, processor]
    pargs.extend(["--input=inputMd5s"])
    subprocess.Popen(pargs)
    obj = {
        "code": 200,
    }

    return jsonify(obj)

@func.route('/hand_detect/virtual_mouse', methods=['GET'])
def virtual_mouse():
    if not os.path.exists(INTERPRETER):
        log.error("Cannot find INTERPRETER at path \"%s\"." % INTERPRETER)

    processor = "./hand_app/VolumeHandControl/HandTrackingModule.py"
    pargs = [INTERPRETER, processor]
    pargs.extend(["--input=inputMd5s"])
    subprocess.Popen(pargs)
    obj = {
        "code": 200,
    }

    return jsonify(obj)

@func.route('/hand_detect/virtual_painter', methods=['GET'])
def virtual_painter():
    if not os.path.exists(INTERPRETER):
        log.error("Cannot find INTERPRETER at path \"%s\"." % INTERPRETER)

    processor = "./hand_app/VolumeHandControl/HandTrackingModule.py"
    pargs = [INTERPRETER, processor]
    pargs.extend(["--input=inputMd5s"])
    subprocess.Popen(pargs)

    obj = {
        "code": 200,
    }

    return jsonify(obj)

@func.route('/hand_detect/volumn_control', methods=['GET'])
def volumn_control():
    if not os.path.exists(INTERPRETER):
        log.error("Cannot find INTERPRETER at path \"%s\"." % INTERPRETER)

    processor = "./hand_app/VolumeHandControl/VolumeHandControl.py"
    pargs = [INTERPRETER, processor]
    pargs.extend(["--input=inputMd5s"])
    subprocess.Popen(pargs)
    obj = {
        "code": 200,
    }

    return jsonify(obj)

# 实际访问url为ip:port/v0001/hello/post,只接受POST方法
@func.route('/hello/post', methods=['POST'])
def hello_post():
    data = request.get_data(as_text=True)
    print("recv post data: ", data)
    obj = {
        "code": 200,
    }

    return jsonify(obj)